package com.fujitsu.cn.fnst.service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.cn.fnst.base.BaseService;
import com.fujitsu.cn.fnst.dao.MeetingBookDao;
import com.fujitsu.cn.fnst.entiy.MeetingBook;
import com.fujitsu.cn.fnst.model.MeetingModel;

@Transactional
@Service
public class MeetingBookService implements BaseService<MeetingBook> {
    @Resource
    private MeetingBookDao meetingBookDao;
    @Resource
    private MeetingUseService meetingUseService;
    @Resource
    private UserService userService;

    @Override
    public MeetingBook find(int id) {

        return meetingBookDao.query(MeetingBook.class, id);
    }

    @Override
    public MeetingBook modify(MeetingBook t) {
        return meetingBookDao.update(t);
    }

    @Override
    public void delete(MeetingBook t) {
        meetingBookDao.delete(t);
    }

    @Override
    public MeetingBook add(MeetingBook t) {
        return meetingBookDao.insert(t);
    }

    @Override
    public List<MeetingBook> find(String queryString) {
        return meetingBookDao.query(queryString);
    }

    public List<MeetingModel> getMeetingState(String where, String order) {
        return meetingBookDao.getMeetingState(where, order);
    }

    public MeetingModel getMeetingState(int meetingId) {
        return meetingBookDao.getMeetingState(meetingId);
    }

    @SuppressWarnings("unused")
    @Deprecated
    private String timeModel(String date, String beginTime, String endTime,
            String amount) throws ParseException {

        return meetingBookDao.timeModel(date, beginTime, endTime, amount);
    }

    /**
     * 预约会议室方法，预约成功返回true,不成功返回false
     * 
     * @param workerId
     * **/
    public boolean bookMeetingRoom(int userId, String workerId, Date startTime,
            Date endTime, int meetingId, String title, String details) {
        if (!meetingUseService.checkMeetingisVaild(startTime, endTime,
                meetingId)) {
            meetingBookDao.bookMeetingRoom(startTime, endTime, meetingId,
                    userId, workerId, title, details);
            meetingUseService.bookMeetingRoom(startTime, endTime, meetingId);
            return true;
        }
        return false;

    }

    public List<MeetingBook> getMeetingRoomHistroy(int meetingId) {
        return meetingBookDao.getMeetingRoomHistroy(meetingId);
    }

    public List<MeetingBook> getUserHistroy(int useId) {
        return meetingBookDao.getUserHistroy(useId);
    }

    public String getJSON(Date startTime, Date endTime, int userId) {
        return meetingBookDao.getJSON(startTime, endTime, userId);

    }

    public List<MeetingBook> getBooks(Date startTime, Date endTime, int userId) {
        return meetingBookDao.getBooks(startTime, endTime, userId);

    }

    public List<MeetingBook> getBooks(Date startTime, Date endTime, int userId,
            int meetingId) {
        return meetingBookDao.getBooks(startTime, endTime, userId, meetingId);
    }

    public boolean deleteBook(int userId, int bookId) {
        MeetingBook book = find(bookId);
        if (book == null
                || (userId != book.getUserId() && userService.isAdmin(userId))
                && book.getBookStartTime().after(new Date())) {
            // 如果查询不到,表示数据有误,或者不可以删除
            return false;
        }
        meetingUseService.deleteBook(book);
        meetingBookDao.delete(book);
        return true;

    }

    public List<MeetingBook> getBooks(Date startTime, int userID) {
        return meetingBookDao.getBooks(startTime, userID);

    }

    public List<MeetingBook> getHisBooks(Date endTime, int userID) {
        return meetingBookDao.getHisBooks(endTime, userID);
    }

    public List<MeetingBook> getAllMeetingBooks() {
        return meetingBookDao.getAllMeetingBooks();
    }

    public List<Map<Integer, Float>> getTopFiveUsedMeetingRooms(Date beginDate,
            Date endDate) {
        return meetingBookDao.getTopFiveUsedMeetingRooms(beginDate, endDate);
    }

    public List<Map<Integer, Float>> getTopFiveUsedPerson(Date beginDate,
            Date endDate) {
        return meetingBookDao.getTopFiveUsedPerson(beginDate, endDate);
    }
    
    public MeetingBook getMeetingBook(int meetingId,Date date,float clock){
    	return meetingBookDao.getMeetingBook(meetingId,date,clock);
    }

	public List<MeetingBook> getAllMeetingsByTime(Date endTime) {
		return meetingBookDao.getAllMeetingsByTime( endTime) ;
	}

}
